const api = require("../../utils/api.js").default;
// 引入封装好的请求方式
const myRequest = require("../../utils/request.js").default;

// pages/shopcart/shopcart.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    cartlist:[],
    cartTotal:{},
    isAllSelect: false,
    totalMoney: 0,
    hascartData:false,
    cart_select: api.imgUrl + 'cart_select.png',
    cart_normal: api.imgUrl + 'cart_normal.png',
    cart_add: api.imgUrl + 'cart_add.png',
    cart_red: api.imgUrl + 'cart_red.png'
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getcartList();
  },


  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    wx.showLoading({
      title: '正在加载您的购物车',
      success: this.getcartList()
    })
  },


  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    this.getcartList();
  },


  // 设置购物车数据
  setCartData(res) {
    const that = this;
    that.setData({
      cartlist: res.data.data.cartList,
      cartTotal: res.data.data.cartTotal
    });
    if (that.data.cartlist.length !== 0) {
      that.setData({
        hascartData: true
      })
      for (var i = 0; i < that.data.cartlist.length; i++){
        if (that.data.cartlist[i].checked == 1){
          that.setData({
            isAllSelect: true
          })
        }else{
          that.setData({
            isAllSelect: false
          })
          break;
        }
      }
    }else{
      that.setData({
        hascartData: false,
        isAllSelect:false
      })
    }
      
  },

  // 获取购物车数据
  getcartList(){
    const that = this;
    myRequest(api.cart, 'GET', {}, function (res) {
      console.log("成功拿到购物车数据", res);
      that.setCartData(res);
      wx.hideLoading();
    });
  },

  // 增加购物车数据
  addShop(){
    const that = this;
    myRequest(api.addcart, "POST", { goodsId: "1151013", number: "1", productId:"230"},function(res){
      that.setCartData(res);
    })
  },

  // 更新购物车数据
  updatecartList(id, goodsId, itemnumber, productId){
    const that = this;
    myRequest(api.updateCart, "POST", { id: id, goodsId: goodsId, number: itemnumber, productId: productId}, function (res) {
      that.setCartData(res);
    })
  },

  // 删除购物车数据
  deletecarList(e){
    const that = this;
    var productid = e.currentTarget.dataset.productid;
    productid = productid.toString();
    myRequest(api.delcart, "POST", { productIds: productid},function(res){
      that.setCartData(res);
    })
  },

  // 减少商品数目功能
  itemRed(e) {
    const that = this;
    var thatitem = e.currentTarget.dataset.thatitem;
    var goodsId = thatitem.goods_id;
    var id = thatitem.id;
    var itemnumber = thatitem.number - 1;
    var productId = thatitem.product_id;
    if (itemnumber <= 0 ){
      // console.log("输出询问是否删除该商品");
      wx.showModal({
        title: 'tips',
        content: '再减就要删掉我了喔',
        success:function(res){
          if(res.confirm){
            productId = productId.toString();
            myRequest(api.delcart, "POST", {
              productIds: productId
            },function(res){
              that.setCartData(res);
            })
          }
          else if(res.cancel){
            itemnumber = 1;
            that.updatecartList(id, goodsId, itemnumber, productId);
          }
        }
      })
    }
    this.updatecartList(id, goodsId, itemnumber, productId);
  },
  // 增加商品数目功能
  itemAdd(e) {
    var thatitem = e.currentTarget.dataset.thatitem;
    var goodsId = thatitem.goods_id;
    var id = thatitem.id;
    var itemnumber = thatitem.number + 1;
    var productId = thatitem.product_id;
    if (itemnumber > 999) {
      console.log("输出提示框说大于商品总数了");
      itemnumber = 999;
    }
    this.updatecartList(id, goodsId, itemnumber, productId);
  },

  // 选中和取消选中商品功能
  checkeditem(e){
    const that = this;
    var productid = e.currentTarget.dataset.productids;
    var itemIsChecked = e.currentTarget.dataset.checked;
    if (itemIsChecked == 0){
      itemIsChecked = 1;
    }else{
      itemIsChecked = 0;
    }
    myRequest(api.cheCart, "POST", { productIds: productid, isChecked: itemIsChecked},function(res){
      console.log('选中', res);
      that.setCartData(res);
    })
  },

  // 全选商品功能
  allSelect(){
    const that = this;
    // 获取购物车数据
    myRequest(api.cartList, 'GET',{},function(res){
      var proidarr = [];
      var proidstr = "";
      var isCheckeds = 1;
      that.setData({
        cartlist: res.data.data.cartList,
        cartTotal: res.data.data.cartTotal
      });
      if (that.data.isAllSelect == false) {
        for (var i = 0; i < that.data.cartlist.length; i++) {
          if (that.data.cartlist[i].checked == 0) {
            proidarr.push(that.data.cartlist[i].product_id);
          }
        }
        proidstr = proidarr.join();
        isCheckeds = 1;
      } else {
        for (var i = 0; i < that.data.cartlist.length; i++) {
          if (that.data.cartlist[i].checked == 1) {
            proidarr.push(that.data.cartlist[i].product_id);
          }
        }
        proidstr = proidarr.join();
        isCheckeds = 0;
      }
        // 刷新购物车列表
      myRequest(api.cheCart,"POST",{
        productIds: proidstr,
        isChecked: isCheckeds
      },function(res){
        that.setCartData(res);
      })
    });
  },

  // 结算
  toBuy(){
    // if(){
      
    // }
    wx.navigateTo({
      url: '/pages/toPay/toPay',
    })
  }
})